using AutoMapper;
using Core.SimpleTemp.Common;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Entitys;
using Core.SimpleTemp.Repositories;
using Core.SimpleTemp.Repositories.Internal;
using Core.Toolkit.Common;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Core.Toolkit.Core.Application;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Application
{
    [AutoDi()]
    public class YH_ScreeningDetailService : BaseAppService<YH_ScreeningDetailDto, YH_ScreeningDetail, YH_ScreeningDetailRepository>
    {
        readonly ExcelHelp _excelHelp;
        readonly CoreDBContext _dBContext;
        public YH_ScreeningDetailService(CoreDBContext dBContext, YH_ScreeningDetailRepository repository, ExcelHelp excelHelp) : base(repository)
        {
            _excelHelp = excelHelp;
            _dBContext = dBContext;
        }



        private LoadPageOffsetModelAuto<YH_ScreeningDetail, YH_ScreeningDetailDto>
            GetLoadPageOffsetMode(PagingModel<YH_ScreeningDetailDto>
                pagingQueryModel)
        {
            var loadPageOffsetModel = this.GetLoadPageOffsetModelAuto(pagingQueryModel);
            var baseQuery = _repository.QueryBase().AsNoTracking();
            loadPageOffsetModel.QueryWhere = this.CreateQRList()

     .Add("ReportType", "=") //上报分类(0计划/1非计划)
     .Add("ScreeningPlanProDetailId", "=") //排查计划项目明细ID
     .Add("InspectTime", "=") //检查时间
     .Add("StandardID", "=") //排查标准ID
     .Add("StandardContent", "=") //排查标准内容
     .Add("Type", "=") //标准/手动
     .Add("result", "=") //排查结果
     .Add("StandingBookId", "=") //隐患台账ID
     .Add("Status", "=") //状态(保存/提交)
     .Add("ClearanceProjects", "=") //排查项目
     .Add("AreaID", "=") //区域
     .Add("LocationID", "=") //位置
     .Add("PersonID", "=") //排查人
     .Add("Time", "=") //排查时间
     .Add("CreateFactory", "=") //
     .Add("CreateDept", "=") //
     .Add("UpdateDept", "=");//


            loadPageOffsetModel.BasqQuery = baseQuery;
            loadPageOffsetModel.Order = orderModel => orderModel.CreateTime;
            //loadPageOffsetModel.Selector = model => new YH_ScreeningDetail()
            //{
            //    Id = model.Id,
            //    ReportType = model.ReportType,//上报分类(0计划/1非计划)
            //    ScreeningPlanProDetailId = model.ScreeningPlanProDetailId,//排查计划项目明细ID
            //    InspectTime = model.InspectTime,//检查时间
            //    StandardID = model.StandardID,//排查标准ID
            //    StandardContent = model.StandardContent,//排查标准内容
            //    Type = model.Type,//标准/手动
            //    result = model.result,//排查结果
            //    StandingBookId = model.StandingBookId,//隐患台账ID
            //    Status = model.Status,//状态(保存/提交)
            //    ClearanceProjects = model.ClearanceProjects,//排查项目
            //    AreaID = model.AreaID,//区域
            //    LocationID = model.LocationID,//位置
            //    PersonID = model.PersonID,//排查人
            //    Time = model.Time,//排查时间
            //    CreateFactory = model.CreateFactory,//
            //    CreateDept = model.CreateDept,//
            //    UpdateDept = model.UpdateDept,//
            //};
            return loadPageOffsetModel;
        }


        public async Task<IPageModel<YH_ScreeningDetailDto>> GetList(PagingModel<YH_ScreeningDetailDto>
            pagingQueryModel)
        {
            var loadPageOffsetModel = GetLoadPageOffsetMode(pagingQueryModel);
            return await base.LoadPageOffsetAsync(loadPageOffsetModel);
        }

        public async Task<YH_ScreeningDetailDto> GetAsync(Guid id)
        {
            var baseQuery = _repository.QueryBase().AsNoTracking()
                .Include(a => a.YH_StandingBook);
            var result = await GetAsync(id, baseQuery);


            //此处是硬从父级的父级表中获取StandardTypeText内容，回头再优化吧。
            var screeningPrjoect =await _dBContext.Set<YH_ScreeningProject>().Where(a => a.Id == result.ScreeningProjectId).FirstOrDefaultAsync();
            var screening =await _dBContext.Set<YH_Screening>().Where(a => a.Id == screeningPrjoect.ScreeningId).FirstOrDefaultAsync();

            YH_ScreeningDetailDto res= Mapper.Map<YH_ScreeningDetailDto>(result);
            res.StandardTypeText = Mapper.Map<YH_ScreeningDto>(screening).StandardTypeText;
            res.ScreeningId = screening.Id;
            res.ScreeningProjectId = screeningPrjoect.Id;

            return res;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dtoList"></param>
        /// <returns></returns>
        public async Task<bool> SaveListAsync(List<YH_ScreeningDetailDto> dtoList)
        {
            var entityList = Mapper.Map<List<YH_ScreeningDetail>>(dtoList);
            await _repository.Set.AddRangeAsync(entityList);
            await _repository.SaveAsync();
            return true;
        }
    }
}
